﻿using System.Net;
using System.Text;
using System.Xml;

namespace JWTAPI.SMSInfo;

public class SMS
{

     #region 短信属性
       /// <summary>
        /// 发送的号码
        /// </summary>
        public string SmsSendNumber { get; set; }

        /// <summary>
        /// 短信主题
        /// </summary>
        public string SmsSubject { get; set; }

        /// <summary>
        /// 短信后缀
        /// </summary>
        public string SmsSuffix { get; set; }

        /// <summary>
        /// 短信内容
        /// </summary>
        public string SmsContent { get; set; }

        /// <summary>
        /// 发送到的号码
        /// </summary>
        public string ReceiveNumber { get; set; }


        /// <summary>
        /// 是否需要回复
        /// </summary>
        public string IsNeedReply { get; set; }


        /// <summary>
        /// 回复的ID
        /// </summary>
        public string ReplyId { get; set; }

        /// <summary>
        /// 回复的描述
        /// </summary>
        public string ReplyDesc { get; set; }


        /// <summary>
        /// 是否定时发送
        /// </summary>
        public string IsTimeSend { get; set; }


        /// <summary>
        /// 定时发送的时间
        /// </summary>
        public string SendTime { get; set; }


        /// <summary>
        /// 调用方服务器IP
        /// </summary>
        public string IpAddress { get; set; }

        /// <summary>
        /// 调用方Mac地址
        /// </summary>
        public string MacAddress { get; set; }
       #endregion

    //发送请求
     public string SendRequest()
    {

        string str = "";
        //双方协定的XML格式 
        Encoding encoding = Encoding.UTF8;
        //接收页面　　　　　　　

        string strUrl = "http://61.178.121.14:8081/wwsmp/services/GatherAccSyncService";

        string _da = CreateXml();

        byte[] data = encoding.GetBytes(_da);
        //准备请求...
        HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create(strUrl);

        myRequest.Method = "POST";

        myRequest.ContentType = "text/xml;charset=UTF-8";

        myRequest.ContentLength = data.Length;

        Stream newStream = myRequest.GetRequestStream(); //发送数据 
        newStream.Write(data, 0, data.Length); newStream.Close();
        WebResponse response = myRequest.GetResponse();
        Stream resStream = response.GetResponseStream();
        StreamReader sr = new StreamReader(resStream, System.Text.Encoding.UTF8);
        str = sr.ReadToEnd();//接收返回值，返回值可以是xml 
        resStream.Close();
       
        sr.Close();

        return str;
    }
    
     //创建XML
     protected string CreateXml()
        {

            XmlDocument doc = new XmlDocument();

            XmlDeclaration dec = doc.CreateXmlDeclaration("1.0", "UTF-8", null);


            doc.AppendChild(dec);

            //创建一个根节点
            XmlNode root = doc.CreateElement("Root");

            doc.AppendChild(root);

            XmlNode _timestamp = doc.CreateElement("TimeStamp");

            _timestamp.InnerText = GetTimeStamp();

            root.AppendChild(_timestamp);

            XmlNode _transactionID = doc.CreateElement("TransactionID");

            _transactionID.InnerText = _timestamp.InnerText + "01001";

            root.AppendChild(_transactionID);

            //短信类型
            XmlNode _smsType = doc.CreateElement("SmsType");

            _smsType.InnerText = "000"; //我们是普通短信

            root.AppendChild(_smsType);

            XmlNode _smsPriority = doc.CreateElement("SmsPriority");

            _smsPriority.InnerText = "3";  //我们挺重要

            root.AppendChild(_smsPriority);

            XmlNode _smsSendNumber = doc.CreateElement("SmsSendNumber"); //扩展sp号码

            _smsSendNumber.InnerText = this.SmsSendNumber;

            root.AppendChild(_smsSendNumber);

            XmlNode _smsSubject = doc.CreateElement("SmsSubject");

            _smsSubject.InnerText = this.SmsSubject; //短信主题

            root.AppendChild(_smsSubject);

            XmlNode _smsSuffix = doc.CreateElement("SmsSuffix");

            _smsSuffix.InnerText = this.SmsSuffix; //短信后缀

            root.AppendChild(_smsSuffix);

            XmlNode _smsContent = doc.CreateElement("SmsContent");

            _smsContent.InnerText = this.SmsContent;

            root.AppendChild(_smsContent);

            XmlNode _receiveNumber = doc.CreateElement("ReceiveNumber");

            _receiveNumber.InnerText = this.ReceiveNumber;

            root.AppendChild(_receiveNumber);

            XmlNode _isNeedReply = doc.CreateElement("IsNeedReply");

            _isNeedReply.InnerText = this.IsNeedReply;

            root.AppendChild(_isNeedReply);

            XmlNode _replyId = doc.CreateElement("ReplyId");

            _replyId.InnerText = this.ReplyId;

            root.AppendChild(_replyId);

            XmlNode _replyDesc = doc.CreateElement("ReplyDesc");

            _replyDesc.InnerText = this.ReplyDesc;

            root.AppendChild(_replyDesc);

            XmlNode _isTimeSend = doc.CreateElement("IsTimeSend");

            _isTimeSend.InnerText = this.IsTimeSend;

            root.AppendChild(_isTimeSend);

            XmlNode _SendTime = doc.CreateElement("SendTime");

            _SendTime.InnerText = this.SendTime;

            root.AppendChild(_SendTime);

            XmlNode _createTime = doc.CreateElement("CreateTime");

            _createTime.InnerText = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");

            root.AppendChild(_createTime);

            XmlNode _ipAddress = doc.CreateElement("IpAddress");

            _ipAddress.InnerText = this.IpAddress;

            root.AppendChild(_ipAddress);

            XmlNode _macAddress = doc.CreateElement("MacAddress");

            _macAddress.InnerText = this.MacAddress;

            root.AppendChild(_macAddress);



            string _result = Head(doc.InnerXml);


            return _result;
        }
     
     
     /// <summary>
     /// 生成要发送的XML头
     /// </summary>
     /// <param name="soapMsg"></param>
     /// <returns></returns>
     private string Head(string soapMsg)
     {

         String tsoapMsg = "<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:v3=\"http://www.qhyt.cn/v3.0/\">"
                           + "<soapenv:Header/><soapenv:Body><v3:subAppSmsInfo><xmlString><![CDATA[" + soapMsg
                           + "]]></xmlString></v3:subAppSmsInfo></soapenv:Body></soapenv:Envelope>";

         return tsoapMsg;
     }
     
     
     /// <summary>
     /// 对时间数据进行处理
     /// </summary>
     /// <param name="i"></param>
     /// <returns></returns>
     private string GetFormatData(int i)
     {

         string _count = string.Empty;

         if (i == 0) _count = "00";

         else if (i > 0 && i < 10) _count = "0" + i.ToString();

         else _count = i.ToString();

         return _count;

     }
     
     
     /// <summary>
     /// 事务序列号
     /// </summary>
     /// <returns></returns>
     public string GetTransactionID(int growID)
     {

         string _year = GetFormatData(DateTime.Now.Year);

         string _month = GetFormatData(DateTime.Now.Month);

         string _day = GetFormatData(DateTime.Now.Day);

         string _hour = GetFormatData(DateTime.Now.Hour);

         string _minte = GetFormatData(DateTime.Now.Minute);

         string _second = GetFormatData(DateTime.Now.Second);

         //格式为（YYYYMMDDHHMMSSXXYYY）

         string _Sender = "02"; //发起方一直是本应用

         string _GrowID = growID.ToString(); //自增ID

         string _TransactionID = _year + _month + _day + _hour + _minte + _second + _Sender + _GrowID;

         return _TransactionID;
     }
     
     
     /// <summary>
     /// 获取时间字符串
     /// </summary>
     /// <returns></returns>
     public string GetTimeStamp()
     {
         string _year = GetFormatData(DateTime.Now.Year);

         string _month = GetFormatData(DateTime.Now.Month);

         string _day = GetFormatData(DateTime.Now.Day);

         string _hour = GetFormatData(DateTime.Now.Hour);

         string _minte = GetFormatData(DateTime.Now.Minute);

         string _second = GetFormatData(DateTime.Now.Second);

         string _time = _year + _month + _day + _hour + _minte + _second;

         return _time;
     }
}